using Microsoft.Extensions.Logging;
using ModelContextProtocol.AspNetCore;
using ModelContextProtocol.Server;
using System.ComponentModel;

namespace ConsoleApi.Services;

/// <summary>
/// MCP测试服务 - 用于验证MCP连接
/// </summary>
[McpServerToolType]
public class TestMcpService
{
    private readonly ILogger<TestMcpService> _logger;

    public TestMcpService(ILogger<TestMcpService> logger)
    {
        _logger = logger;
    }

    /// <summary>
    /// 测试连接
    /// </summary>
    [McpServerTool(Name = "test-connection")]
    [Description("测试MCP连接是否正常")]
    public string TestConnection()
    {
        _logger.LogInformation("MCP测试连接被调用");
        return "✅ MCP连接正常！服务运行中...";
    }

    /// <summary>
    /// 获取服务器信息
    /// </summary>
    [McpServerTool(Name = "get-server-info")]
    [Description("获取MCP服务器信息")]
    public string GetServerInfo()
    {
        return "MES天气预警MCP服务器 v1.0.0\n状态：运行中\n时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
    }

    /// <summary>
    /// 简单计算器
    /// </summary>
    [McpServerTool(Name = "calculate")]
    [Description("简单的数学计算")]
    public string Calculate(
        [Description("第一个数字")] double a,
        [Description("运算符 (+,-,*,/)")] string operation,
        [Description("第二个数字")] double b)
    {
        try
        {
            double result = operation switch
            {
                "+" => a + b,
                "-" => a - b,
                "*" => a * b,
                "/" => b != 0 ? a / b : throw new DivideByZeroException(),
                _ => throw new ArgumentException("不支持的运算符")
            };

            return $"计算: {a} {operation} {b} = {result}";
        }
        catch (Exception ex)
        {
            return $"计算错误: {ex.Message}";
        }
    }
} 